package com.micmac.customer.service
{
	import com.AppConst;
	import com.micmac.customer.events.CustomerArrayEvent;
	import com.micmac.customer.events.CustomerEvent;
	import com.micmac.customer.events.CorpEvent;
	import com.micmac.customer.interfaces.ICustomerModule;
	import com.micmac.customer.interfaces.ICustomerService;
	import com.micmac.customer.model.CorpModel;
	import com.micmac.customer.model.OrgModel;
	import com.micmac.customer.model.PackageModel;
	import com.micmac.customer.model.PricingModel;
	import com.micmac.customer.model.ProductModel;
	import com.micmac.customer.model.SalesProcessModel;
	import com.micmac.customer.model.UserModel;
	import com.micmac.customer.vo.CorpVO;
	import com.micmac.customer.vo.OrgProcessVO;
	import com.micmac.customer.vo.OrgVO;
	import com.micmac.customer.vo.PackgeTitleVO;
	import com.micmac.customer.vo.PricingModelVO;
	import com.micmac.customer.vo.PricingTypeVO;
	import com.micmac.customer.vo.ProductFeaturesVO;
	import com.micmac.customer.vo.ProductTitleVO;
	import com.micmac.customer.vo.RegionVO;
	import com.micmac.customer.vo.SalesProcessVO;
	import com.micmac.customer.vo.SalesStageVO;
	import com.micmac.customer.vo.SalesStepVO;
	import com.micmac.customer.vo.UserRoleVO;
	import com.micmac.customer.vo.UserTitleVO;
	import com.micmac.customer.vo.UserVO;
	import com.micmac.main.events.NotificationOnEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	import org.robotlegs.utilities.modular.base.ModuleEventDispatcher;
	
	

	public class CustomerService extends Actor implements ICustomerService
	{
		[Inject]
		public var customerModel:CorpModel;
		[Inject]
		public var userModel:UserModel;
		[Inject]
		public var packageModel:PackageModel;
		[Inject]
		public var productModel:ProductModel;
		[Inject]
		public var pricingModel:PricingModel;
		[Inject]
		public var salesProcessModel:SalesProcessModel;
		[Inject]
		public var orgModel:OrgModel;

		private var customerRemote:RemoteObject;
		
		public function CustomerService()
		{
			customerRemote = new RemoteObject();
			customerRemote.destination = "zend";
		}
		
		/*--------------------------------------------------------------
							For Saving
		--------------------------------------------------------------*/
		public function saveCorp(corpVO:CorpVO):void
		{
			customerModel.corpList.addItem(corpVO);
			dispatch(new CustomerEvent(CustomerEvent.CORP_SAVED));
		}
		
		public function saveRegion(regionVO:RegionVO):void
		{
			//customerModel.regionList.addItem(regionVO);
			dispatch(new CustomerEvent(CustomerEvent.REGION_SAVED));
		}
		
		
		public function saveUserTitle(userTitleVo:UserTitleVO):void
		{
			userModel.userTitleList.addItem(userTitleVo);
			dispatch(new CustomerEvent(CustomerEvent.USER_TITLE_SAVED));
		}
		public function saveUser(userVo:UserVO):void
		{
			userModel.userList.addItem(userVo);
			dispatch(new CustomerEvent(CustomerEvent.USER_SAVED));
		}
		public function saveUserRole(userRoleVO:UserRoleVO):void
		{
			userModel.userRoleList.addItem(userRoleVO);
			dispatch(new CustomerEvent(CustomerEvent.USER_ROLE_SAVED));
		}
		
		public function savePackageTitle(packageTitleVo:PackgeTitleVO):void
		{
			packageModel.packageTitleList.addItem(packageTitleVo);
			dispatch(new CustomerEvent(CustomerEvent.PACKAGE_TITLE_SAVED));
		}
		
		public function saveProductTitle(productTitleVo:ProductTitleVO):void
		{
			productModel.productTitleList.addItem(productTitleVo);
			dispatch(new CustomerEvent(CustomerEvent.PRODUCT_TITLE_SAVED));
		}
		public function saveProductFeature(productFeatureVo:ProductFeaturesVO):void
		{
			productModel.productFeatureList.addItem(productFeatureVo);
			dispatch(new CustomerEvent(CustomerEvent.PRODUCT_FEATURE_SAVED));
		}
		
		public function savePricingModel(pricingModelVO:PricingModelVO):void
		{
			pricingModel.pricingModelList.addItem(pricingModelVO);
			dispatch(new CustomerEvent(CustomerEvent.PRICING_MODEL_SAVED));
		}
		
		public function savePricingType(pricingTypeVO:PricingTypeVO):void
		{
			pricingModel.pricingTypeList.addItem(pricingTypeVO);
			dispatch(new CustomerEvent(CustomerEvent.PRICING_TYPE_SAVED));
		}
		
		public function saveSalesProsess(salesProcessVo:SalesProcessVO):void
		{
			salesProcessModel.salesProcessList.addItem(salesProcessVo);
			dispatch(new CustomerEvent(CustomerEvent.SALES_PROCESS_SAVED));
		}
		
		public function saveSalesStage(salesStageVo:SalesStageVO):void
		{
			salesProcessModel.salesStageList.addItem(salesStageVo);
			dispatch(new CustomerEvent(CustomerEvent.SALES_STAGE_SAVED));
		}
		
		public function saveSalesStep(salesStepVo:SalesStepVO):void
		{
			salesProcessModel.salesStepList.addItem(salesStepVo);
			dispatch(new CustomerEvent(CustomerEvent.SALES_STEP_SAVED));
		}
		
		public function saveOrg(orgVO:OrgVO):void
		{
			orgModel.orgList.addItem(orgVO);
			dispatch(new CustomerEvent(CustomerEvent.ORG_SAVED));
		}
		public function saveOrgProcess(orgProcessVO:OrgProcessVO):void
		{
			orgModel.orgProcessList.addItem(orgProcessVO);
			dispatch(new CustomerEvent(CustomerEvent.ORG_PROCESS_SAVED));
		}
		
		/*--------------------------------------------------------------
							For Editing
		--------------------------------------------------------------*/
			
		public function editCorp(corpVO:CorpVO):void
		{
			dispatch(new CorpEvent(CorpEvent.CORP_EDIT_FROM_SERVICE,corpVO));
		}
		
		public function deleteCorp(corpVO:CorpVO):void
		{
			dispatch(new CorpEvent(CorpEvent.CORP_DELETE_FROM_SERVICE,corpVO));
		}
		
		public function getRegions():void
		{
			customerRemote.source = "RegionSvc";
			customerRemote.getRegionData.addEventListener(ResultEvent.RESULT,onResultRegions);
			customerRemote.getRegionData.addEventListener(FaultEvent.FAULT,onFault);
			customerRemote.getRegionData();
		}
		
		public function getIndustryType():void
		{
			customerRemote.source = "IndustryTypeSICSvc";
			customerRemote.getIndustryTypeSICData.addEventListener(ResultEvent.RESULT, onResultIndustryType);
			customerRemote.getIndustryTypeSICData.addEventListener(FaultEvent.FAULT, onFault);
			customerRemote.getIndustryTypeSICData();
		}
		
		public function getIndustry():void
		{
			customerRemote.source = "IndustrySICSvc";
			customerRemote.getIndustrySICData.addEventListener(ResultEvent.RESULT, onResultIndustry);
			customerRemote.getIndustrySICData.addEventListener(FaultEvent.FAULT, onFault);
			customerRemote.getIndustrySICData();
		}
		
		private function onResultRegions(event:ResultEvent):void
		{
			var region:ArrayCollection = new ArrayCollection (event.result as Array);
			dispatch(new CustomerArrayEvent(CustomerArrayEvent.REGION_LIST,region));
		}
		
		private function onResultIndustryType(event:ResultEvent):void
		{
			var industryTypeList:ArrayCollection = new ArrayCollection(event.result as Array);
			dispatch(new CustomerArrayEvent(CustomerArrayEvent.INDUSTRY_TYPE,industryTypeList));
		}
		
		private function onResultIndustry(event:ResultEvent):void
		{
			var industryList:ArrayCollection = new ArrayCollection(event.result as Array);
			dispatch(new CustomerArrayEvent(CustomerArrayEvent.INDUSTRY,industryList));
		}
		
		private function onFault(event:FaultEvent):void
		{
		}
	}
}